import Vue from 'vue'
import App from './App.vue'
import router from './router/index'
import store from './store'

//引入 Element UI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

//引入 vue-awesome-swiper
import VueAwesomeSwiper from 'vue-awesome-swiper'
// require styles
import 'swiper/dist/css/swiper.css'

Vue.use(VueAwesomeSwiper);

// 引入 vue-quill-editor
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor)

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios);

import timeago from 'timeago.js'

import '@/assets/styles/css/border.css'
import '@/assets/styles/css/iconfont.css'
import '@/assets/styles/css/reset.css'

Vue.config.productionTip = false;

//money 过滤器
Vue.filter('money', (val) => {
    if (typeof val === 'number') {
        if (val % 1 === 0) {
            return val + '.00'
        } else {
            return val
        }
    } else {
        return val
    }
});

//  时间过滤器
Vue.filter('changeTime', (val)=>{
    /**
     * @type {number}
     * @val 返回的是一个具体的时间值
     *
     */
    let oldTime = new Date(val).getTime()
    let result;
    let minute = 1000 * 60; //3600000
    let hour = minute * 60;
    let day = hour * 24;
    let month = day * 30;
    let now = new Date().getTime();
    let diffValue = now - oldTime;
    if(diffValue < 0){
        return;
    }
    let monthC =diffValue/month;
    let weekC =diffValue/(7*day);
    let dayC =diffValue/day;
    let hourC =diffValue/hour;
    let minC =diffValue/minute;
    if(monthC>=1){
        if(monthC<=12)
            result="" + parseInt(monthC) + "月前";
        else{
            result="" + parseInt(monthC/12) + "年前";
        }
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else{
        result="刚刚";
    }
    return result;
});




// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
        if (localStorage.getItem('userToken')) {
            config.headers.Authorization =localStorage.getItem('userToken');
        }
        console.log(localStorage.getItem('userToken'));
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app');
